package com.kky.pp.activity.index

import android.app.Activity
import android.graphics.drawable.Drawable
import android.util.Log
import com.bumptech.glide.Glide
import com.bumptech.glide.load.DataSource
import com.bumptech.glide.load.engine.DiskCacheStrategy
import com.bumptech.glide.load.engine.GlideException
import com.bumptech.glide.request.RequestListener
import com.bumptech.glide.request.target.Target
import com.kky.pp.bean.CardModelBean
import com.kky.pp.util.CollectionUtils

object HomeCardPreloadHelper {


    //第一档的预加载 数量
    val onePreloadSize = 6

    //第二档的预加载 数量
    val twoPreloadSize = 10

    //预加载卡片数量
    var num = 0
    var loadFialy = 0
    fun preloadPic(activity: Activity, homepageList: List<CardModelBean>) {
        num = 0
        val oneDan = getOneDan(homepageList)
        val oneTwoDan = getTwoDan(homepageList)
        val time = System.currentTimeMillis()
        loadPic(activity, oneDan) {
            //全部缓存完成
        }
    }

    fun adapterPreloadLoad(activity: Activity, position: Int, homepageList: List<CardModelBean>) {
        try {
            val matchIndexBean = homepageList.get(position + 3)
            val arrayList = arrayListOf(matchIndexBean.images.get(0).imageUrl)
            loadPic(activity,arrayList){

            }
        } catch (e: Exception) {
            e.printStackTrace()
        }
    }

    private fun loadPic(
        activity: Activity,
        homepageList: List<String>,
        loadEnd: () -> Unit
    ) {
        var maxSize = homepageList.size
        for (i in 0 until maxSize) {
            num++
            Glide.with(activity)
                .load(homepageList.get(i))
                .diskCacheStrategy(DiskCacheStrategy.ALL)
                .addListener(object : RequestListener<Drawable> {
                    override fun onLoadFailed(
                        e: GlideException?,
                        model: Any?,
                        target: Target<Drawable>?,
                        isFirstResource: Boolean
                    ): Boolean {
                        loadFialy++
                        maxSize--
                        if (maxSize <= 0) {
                            loadEnd?.invoke()
                        }
                        Log.d("11111111", homepageList.get(i))
                        Glide.with(activity)
                            .load(homepageList.get(i))
                            .diskCacheStrategy(DiskCacheStrategy.ALL)
                            .preload()
                        if (maxSize <= 0) {
                            loadEnd?.invoke()
                        }
                        return false
                    }

                    override fun onResourceReady(
                        resource: Drawable?,
                        model: Any?,
                        target: Target<Drawable>?,
                        dataSource: DataSource?,
                        isFirstResource: Boolean
                    ): Boolean {
                        maxSize--
                        if (maxSize <= 0) {
                            loadEnd?.invoke()
                        }
                        return false
                    }

                })
                .preload()
        }
    }

    private fun getOneDan(homepageList: List<CardModelBean>): ArrayList<String> {
        val preloadSize = 6
        val lists = ArrayList<String>()
        for (i in 0 until homepageList.size) {
            if (i > preloadSize)
                return lists
            if (CollectionUtils.isNotEmpty(homepageList[i].images)) {
                for (k in 0 until homepageList[i].images.size) {
                    if (i == 0) {
                        //第一张卡片缓存卡片全部图片
                        if (k > 2) {
                            break
                        }
                        lists.add(homepageList[i].images[k].imageUrl)
                    } else {
                        if (k > 1) {
                            break
                        }
                        //非第一张卡片预加载 卡片第一张图片
                        lists.add(homepageList[i].images[k].imageUrl)
                    }
                }
            }
        }
        return lists
    }

    private fun getTwoDan(
        homepageList: List<CardModelBean>
    ): ArrayList<String> {
        val lists = ArrayList<String>()
        for (i in 0 until homepageList.size) {
            if (i >= twoPreloadSize) {
                return lists
            }
            if (CollectionUtils.isNotEmpty(homepageList[i].images)) {
                for (k in 0 until homepageList[i].images.size) {
                    if (i >= onePreloadSize) {
                        lists.add(homepageList[i].images[k].imageUrl)
                    } else {
                        if (k > 1) {
                            //非第一张卡片预加载 卡片第一张图片
                            lists.add(homepageList[i].images[k].imageUrl)
                            break
                        }
                    }
                }
            }
        }
        return lists
    }

}